<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="80px">
            <el-form-item label="名称" prop="stockName">
                <el-input v-model="queryParams.stockName" placeholder="请输入库存名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="型号" prop="stockType">
                <el-input v-model="queryParams.stockType" placeholder="请输入硬件或产品型号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="封装" prop="stockPackage">
                <el-input v-model="queryParams.stockPackage" placeholder="请输入封装" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item label="负责人" prop="stockManager">
                <el-select v-model="queryParams.stockManager" placeholder="请选择负责人" clearable>
                    <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item> -->
            <!-- <el-form-item label="库存数量" prop="stockNum">
              <el-input v-model="queryParams.stockNum" placeholder="请输入库存数量" clearable
                  @keyup.enter.native="handleQuery" />
          </el-form-item> -->
            <el-form-item label="一级位置" prop="stockPositionA">
                <el-input v-model="queryParams.stockPositionA" placeholder="请输入上级存放位置" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="二级位置" prop="stockPositionB">
                <el-input v-model="queryParams.stockPositionB" placeholder="请输入下级存放位置" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="类别" prop="stockClass">
                <el-select v-model="queryParams.stockClass" placeholder="请选择库存类别，区别与型号" clearable>
                    <el-option v-for="dict in dict.type.stock_type" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item> -->
            <!-- <el-form-item label="入库时间">
                <el-date-picker v-model="daterangeRecordTime" style="width: 240px" value-format="yyyy-MM-dd"
                    type="daterange" range-separator="-" start-placeholder="开始日期"
                    end-placeholder="结束日期"></el-date-picker>
            </el-form-item> -->

            <!-- <el-form-item label="预留字段2" prop="stockMsg">
              <el-input v-model="queryParams.stockMsg" placeholder="请输入预留字段2" clearable
                  @keyup.enter.native="handleQuery" />
          </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:stock:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:stock:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:stock:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:stock:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="id" align="center" prop="id" /> -->
            <el-table-column label="名称" align="center" prop="stockName" />
            <el-table-column label="型号" align="center" prop="stockType" />
            <el-table-column label="封装" align="center" prop="stockPackage" />
            <el-table-column label="负责人" align="center" prop="stockManager">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.lyner_emp" :value="scope.row.stockManager" />
                </template>
            </el-table-column>
            <el-table-column label="库存数量" align="center" prop="stockNum" />
            <el-table-column label="一级位置" align="center" prop="stockPositionA" />
            <el-table-column label="二级位置" align="center" prop="stockPositionB" />
            <el-table-column label="入库时间" align="center" prop="recordTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="类别" align="center" prop="stockClass">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.stock_type" :value="scope.row.stockClass" />
                </template>
            </el-table-column>
            <el-table-column label="库存图片" align="center" prop="stockImg" width="100">
                <template slot-scope="scope">
                    <image-preview :src="scope.row.stockImg" :width="50" :height="50" />
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="stockMsg" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-sell" @click="handleIn(scope.row)">入库 </el-button>
                    <el-button size="mini" type="text" icon="el-icon-sold-out"
                        @click="handleOut(scope.row)">出库</el-button>
                    <el-button size="mini" type="text" icon="el-icon-office-building"
                        @click="handleDetail(scope.row)">详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改库存对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="名称" prop="stockName">
                    <el-input v-model="form.stockName" placeholder="请输入库存名称" />
                </el-form-item>
                <el-form-item label="型号" prop="stockType">
                    <el-input v-model="form.stockType" placeholder="请输入硬件或产品型号" />
                </el-form-item>
                <el-form-item label="封装" prop="stockPackage">
                    <el-input v-model="form.stockPackage" placeholder="请输入封装" />
                </el-form-item>
                <el-form-item label="负责人" prop="stockManager">
                    <el-select v-model="form.stockManager" placeholder="请选择负责人">
                        <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="库存数量" prop="stockNum">
                    <el-input v-model="form.stockNum" placeholder="请输入库存数量" />
                </el-form-item>
                <el-form-item label="一级位置" prop="stockPositionA">
                    <el-input v-model="form.stockPositionA" placeholder="请输入上级存放位置" />
                </el-form-item>
                <el-form-item label="二级位置" prop="stockPositionB">
                    <el-input v-model="form.stockPositionB" placeholder="请输入下级存放位置" />
                </el-form-item>
                <!-- <el-form-item label="最初入库时间" prop="recordTime">
                  <el-date-picker clearable v-model="form.recordTime" type="date" value-format="yyyy-MM-dd"
                      placeholder="请选择最初入库时间">
                  </el-date-picker>
              </el-form-item> -->
                <el-form-item label="库类别" prop="stockClass">
                    <el-select v-model="form.stockClass" placeholder="请选择库存类别，区别与型号">
                        <el-option v-for="dict in dict.type.stock_type" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="库存图片">
                    <image-upload v-model="form.stockImg" />
                </el-form-item>
                <el-form-item label="备注" prop="stockMsg">
                    <el-input v-model="form.stockMsg" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 领用对话框 -->
        <el-dialog :title="title" :visible.sync="use" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="130px">
                <!-- <el-form-item label="名称" prop="stockName">
                  <el-input v-model="form.stockName" placeholder="请输入名称" style="width:300px" :disabled="true" />
              </el-form-item>
              <el-form-item label="负责人" prop="stockManager">
                  <el-select v-model="form.stockManager" placeholder="请选择负责人" :disabled="true">
                      <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item> -->
                <el-form-item label="库存数量" prop="stockNum">
                    <el-input v-model="form.stockNum" placeholder="请输入库存数量" style="width:300px" :disabled="true" />
                </el-form-item>
                <el-form-item label="领用数量" prop="stockNum" :rules="[
                    { required: true, message: '出库数量不能为空' },
                    { type: 'number', message: '出库数量必须为数字值' }
                ]">
                    <el-input v-model="count" placeholder="请输入领用数量" style="width:300px" />
                </el-form-item>
                <!-- <el-form-item label="一级位置" prop="stockPositionA">
                  <el-input v-model="form.stockPosition" placeholder="请输入物品存放位置" style="width:300px"
                      :disabled="true" />
              </el-form-item>
              <el-form-item label="二级位置" prop="stockPositionB">
                  <el-input v-model="form.stockPosition" placeholder="请输入物品存放位置" style="width:300px"
                      :disabled="true" />
              </el-form-item>
              <el-form-item label="类别" prop="stockType">
                  <el-select v-model="form.stockType" placeholder="请选择类别" :disabled="true">
                      <el-option v-for="dict in dict.type.stock_type" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="领用时间" prop="recordTime">
                  <el-date-picker clearable v-model="this.now" type="date" value-format="yyyy-MM-dd"
                      placeholder="请选择最初入库时间">
                  </el-date-picker>
              </el-form-item>

              <el-form-item label="领用人" prop="lastMan">
                  <el-select v-model="form.lastMan" placeholder="请选择最后取用/入库人">
                      <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item> -->
                <el-form-item label="备注" prop="stockNum">
                    <el-input v-model="whyout" placeholder="出库原因" style="width:300px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="outStock">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 入库对话框  -->
        <el-dialog :title="title" :visible.sync="In" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="130px">
                <!-- <el-form-item label="名称" prop="stockName">
                  <el-input v-model="form.stockName" placeholder="请输入名称" style="width:300px" :disabled="true" />
              </el-form-item>
              <el-form-item label="负责人" prop="stockManager">
                  <el-select v-model="form.stockManager" placeholder="请选择负责人" :disabled="true">
                      <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item> -->
                <el-form-item label="库存数量" prop="stockNum">
                    <el-input v-model="form.stockNum" placeholder="请输入库存数量" style="width:300px" :disabled="true" />
                </el-form-item>
                <el-form-item label="入库数量" prop="stockNum" :rules="[
                    { required: true, message: '入库数量不能为空' },
                    { type: 'number', message: '入库数量必须为数字值' }
                ]">
                    <el-input v-model="InCount" placeholder="请输入入库数量" style="width:300px" />
                </el-form-item>
                <!-- <el-form-item label="一级位置" prop="stockPositionA">
                  <el-input v-model="form.stockPosition" placeholder="请输入物品存放位置" style="width:300px"
                      :disabled="true" />
              </el-form-item>
              <el-form-item label="二级位置" prop="stockPositionB">
                  <el-input v-model="form.stockPosition" placeholder="请输入物品存放位置" style="width:300px"
                      :disabled="true" />
              </el-form-item>
              <el-form-item label="类别" prop="stockType">
                  <el-select v-model="form.stockType" placeholder="请选择类别" :disabled="true">
                      <el-option v-for="dict in dict.type.stock_type" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="入库时间" prop="recordTime">
                  <el-date-picker clearable v-model="this.now" type="date" value-format="yyyy-MM-dd"
                      placeholder="请选择入库时间">
                  </el-date-picker>
              </el-form-item>

              <el-form-item label="入库人" prop="lastMan">
                  <el-select v-model="form.lastMan" placeholder="请选择最后取用/入库人">
                      <el-option v-for="dict in dict.type.lyner_emp" :key="dict.value" :label="dict.label"
                          :value="dict.value"></el-option>
                  </el-select>
              </el-form-item> -->
                <el-form-item label="备注" prop="stockNum">
                    <el-input v-model="whyin" placeholder="入库原因" style="width:300px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="inStock">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 详情对话框 -->
        <el-dialog :title="title" :visible.sync="op" width="1000px" append-to-body>
            <el-table :data="tableData" style="width: 100%">


                <el-table-column prop="handleMan" label="操作人">
                </el-table-column>
                <el-table-column prop="handleType" label="操作类型">
                </el-table-column>
                <el-table-column prop="handleTime" label="操作时间">
                </el-table-column>
                <el-table-column prop="handleNum" label="数量">
                </el-table-column>
                <el-table-column prop="handleMessage" label="备注">
                </el-table-column>

            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock, useAPI, addAPI, ListAPI } from "@/api/system/stock";

export default {
    name: "Stock",
    dicts: ['lyner_emp', 'stock_type'],
    data() {
        return {
            // 领用弹框
            use: false,
            // 详情弹框
            op: false,
            // 详情
            tableData: [],
            now: this.getNow(),
            // 入库弹框
            In: false,
            // 入库数量
            InCount: '',
             // 出库理由
             whyout: '',
            // 入库理由
            whyin: '',
            // 出库数量
            count: '',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 库存表格数据
            stockList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 预留字段2时间范围
            daterangeRecordTime: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                stockName: null,
                stockType: null,
                stockPackage: null,
                stockManager: null,
                stockNum: null,
                stockPositionA: null,
                stockPositionB: null,
                recordTime: null,
                stockClass: "研发模块",
                stockImg: null,
                stockMsg: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        getNow() {
            var date = new Date();
            var data1 = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
            return data1
        },
        /** 查询库存列表 */
        getList() {
            this.loading = true;
            this.queryParams.params = {};
            if (null != this.daterangeRecordTime && '' != this.daterangeRecordTime) {
                this.queryParams.params["beginRecordTime"] = this.daterangeRecordTime[0];
                this.queryParams.params["endRecordTime"] = this.daterangeRecordTime[1];
            }
            console.log(this.queryParams);
            listStock(this.queryParams).then(response => {
                this.stockList = response.rows;
                this.total = response.total;
                this.loading = false;
                console.log(response)
            });

        },
        // 取消按钮
        cancel() {
            this.reset();
            this.open = false;
            this.use = false;
            this.In = false;

        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                stockName: null,
                stockType: null,
                stockPackage: null,
                stockManager: null,
                stockNum: null,
                stockPositionA: null,
                stockPositionB: null,
                recordTime: null,
                stockClass: null,
                stockImg: null,
                stockMsg: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.daterangeRecordTime = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加库存";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getStock(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改库存";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                console.log(this.form)
                if (valid) {
                    if (this.form.id != null) {
                        updateStock(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addStock(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        //   领用提交
        outStock() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null && this.count != null) {

                        var p = {

                            id: this.form.id,
                            num: parseInt(this.count),
                            message: this.whyout
                        }
                        useAPI(p).then(response => {
                            this.$modal.msgSuccess("出库成功");
                            // console.log(response);
                            this.use = false;
                            this.getList();
                        });
                    }

                }
            });

        },
        /** 领用按钮操作 */
        handleOut(row) {
            const id = row.id;

            getStock(id).then(response => {
                this.form = response.data;
                this.use = true
                this.title = "物品领用";
            });
        },
        /** 详情按钮操作 */
        handleDetail(row) {
            const id = row.id;
            this.op = true
            ListAPI(id).then(response => {
                // console.log(response);
                this.tableData = response.data;

                this.title = "详情";
            });
        },
        // 入库按钮
        handleIn(row) {
            const id = row.id;

            getStock(id).then(response => {
                this.form = response.data;
                this.In = true
                this.title = "物品入库";
            });
        },
        // 入库按钮操作
        inStock() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null && this.InCount != null) {

                        var p = {
                            id: this.form.id,
                            num: parseInt(this.InCount),
                            message: this.whyin
                        }
                        addAPI(p).then(response => {
                            this.$modal.msgSuccess("入库成功");
                            console.log(response);
                            this.In = false;
                            this.getList();
                        });
                    }

                }
            });

        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除库存编号为"' + ids + '"的数据项？').then(function () {
                return delStock(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/stock/export', {
                ...this.queryParams
            }, `stock_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
